package com.hoshiicloud.marketmanagement.controller;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.DigestUtils;
import com.hoshiicloud.common.utils.ip.IpUtils;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.goods.entity.sku.ProductManage;
import com.hoshiicloud.goods.param.sku.ProductManageAddParam;
import com.hoshiicloud.goods.param.sku.ProductManageQueryParam;
import com.hoshiicloud.market.param.activities.ActiviteGoodsDeleteDto;
import com.hoshiicloud.market.param.activities.ActivitiesProductManageQueryParam;
import com.hoshiicloud.marketmanagement.service.MarketManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author MarkerCCC
 * @date 2019-09-17 - 15:18
 */
@Slf4j
@RestController
@RequestMapping("/marketManagement/marketManagementController")
@Api(value = "活动促销", tags = {"活动促销"})
public class MarketManagementController {


    private final MarketManagementService marketManagementService;

    public MarketManagementController(MarketManagementService marketManagementService) {
        this.marketManagementService = marketManagementService;
    }

    @PostMapping("addMarketActivity")
    @ApiOperation(value = "添加营销活动", notes = "添加营销活动", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void addMarketActivity() {

    }

    /**
     * 添加临时商品记录
     */
    @PostMapping("addTemporaryActivityGoods")
    @ApiOperation(value = "添加活动临时商品", notes = "添加活动临时商品", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<String> addTemporaryActivityGoods(@RequestBody List<ProductManage> productManage,
            HttpServletRequest httpServletRequest) {
        Response<String> response = this.getTemporaryGoodsRedisKey(httpServletRequest);
        String temporaryGoodsRedisKey = response.getData();
        marketManagementService.addTemporaryActivityGoods(productManage, temporaryGoodsRedisKey);
        return Response.data(temporaryGoodsRedisKey);
    }

    @PostMapping("/queryProductManagePage")
    @ApiOperation(value = "查询productManage分页", notes = "查询productManage分页", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response queryProductManagePage(@RequestBody @Valid ActivitiesProductManageQueryParam activitiesProductManageQueryParam) {
        return marketManagementService.queryProductManagePage(activitiesProductManageQueryParam);
    }

    @PostMapping("/queryProductManageAll")
    @ApiOperation(value = "查询productManage所有数据", notes = "查询productManage所有数据", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<List<ProductManage>> queryProductManageAll(
            @RequestBody @Valid ActivitiesProductManageQueryParam activitiesProductManageQueryParam) {
        return marketManagementService.queryProductManageAll(activitiesProductManageQueryParam);
    }

    @GetMapping("getRedisDataPage")
    @ApiOperation(value = "得到添加的活动商品分页", notes = "得到添加的活动商品分页", httpMethod = "GET", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "temporaryGoodsRedisKey", value = "redisKey", example = "12312321321"),
            @ApiImplicitParam(name = "pageNumber", value = "pageNumber", example = "0"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", example = "10")
    })
    public Response<List> getRedisDataPage(@RequestParam("temporaryGoodsRedisKey") String temporaryGoodsRedisKey,
            @RequestParam("pageNumber") Long pageNumber, @RequestParam("pageSize") Long pageSize) {
        List<ProductManage> redisDataPage = marketManagementService.getRedisDataPage(temporaryGoodsRedisKey, pageNumber, pageSize);
        return Response.data(redisDataPage);
    }

    /**
     * 根据登录信息生成零时key
     *
     * @param httpServletRequest httpServletRequest
     * @return redisKey
     */
    @GetMapping("/getTemporaryGoodsRedisKey")
    @ApiOperation(value = "获取RedisKey", notes = "获取RedisKey", httpMethod = "GET")
    public Response<String> getTemporaryGoodsRedisKey(HttpServletRequest httpServletRequest) {
        TokenUsersVO userInfo = UserInfoUtil.getUserInfo(httpServletRequest);
        String realName = userInfo.getRealName();
        Long platId = userInfo.getPlatId();
        Long companyId = userInfo.getCompanyId();
        String storeName = userInfo.getStoreName();
        String ipAddress = IpUtils.getIpAddress(httpServletRequest);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String localTime = df.format(now);
        String redisKey = ipAddress + ":" + localTime + ":" + realName + ":" + platId + ":" + companyId + ":" + storeName;
        return Response.data(DigestUtils.md5(redisKey));
    }

    @DeleteMapping("deleteTemporaryActivityGoods")
    @ApiOperation(value = "批量临时商品", notes = "批量临时商品", httpMethod = "DELETE", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response deleteTemporaryActivityGoods(@RequestBody ActiviteGoodsDeleteDto activiteGoodsDeleteDto) {
        marketManagementService.deleteTemporaryActivityGoods(activiteGoodsDeleteDto);
        return Response.success("删除成功");
    }

    @DeleteMapping("deleteTemporaryActivityGoodsAll")
    @ApiOperation(value = "批量所有临时商品", notes = "批量所有临时商品", httpMethod = "DELETE", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response deleteTemporaryActivityGoodsAll(@RequestBody ActiviteGoodsDeleteDto activiteGoodsDeleteDto) {
        marketManagementService.deleteTemporaryActivityGoodsAll(activiteGoodsDeleteDto);
        return Response.success("删除成功");
    }
}
